--
-- Author: gnar
-- Date: 2015-12-28 13:55:49
--
local NetMsg = import(".network.NetMsg")
local WebSocket = import(".network.WebSocket")
local ManagerNet = class("ManagerNet", WebSocket)
local gInstance = nil


function ManagerNet.getInstance()
	if not gInstance then
		gInstance = ManagerNet.new()
	end

	return gInstance
end

function ManagerNet:onCreate()

	self:on(_netCallback)
	self:onClose(_netClose)
	print("ManagerNet:onCreate()")

end

function ManagerNet:sendCmd(action, data)
	local msg = NetMsg.new()

	msg:setBody(data)
	msg:setAction(action)

	msg:setSession(self:getSessionId())

	self:_send(msg:getString())
end

_netClose = function()
	print("enterScene LoginSceneCCB")
	MyApp:enterScene("LoginSceneCCB")
end

_netCallback = function(data)
	local str = tostring(data)
	if string.len(str) > 1000 then
		str = string.sub(str, 1, 1000)
	end

	local msg = NetMsg.parser(data)

	AdapterSendMsg:sendCallback(msg)

end


return ManagerNet